﻿namespace AC.ExtendedRenderer.Toolkit
{
    using System;
    using System.Collections;
    using System.Windows.Forms;

    public class ListViewColumnSorter : IComparer
    {
        private int ColumnToSort = 0;
        private CaseInsensitiveComparer ObjectCompare = new CaseInsensitiveComparer();
        private SortOrder OrderOfSort = SortOrder.None;

        public int Compare(object x, object y)
        {
            string text;
            string str2;
            ListViewItem item = (ListViewItem) x;
            ListViewItem item2 = (ListViewItem) y;
            try
            {
                text = item.SubItems[this.ColumnToSort].Text;
            }
            catch (Exception exception)
            {
                Console.Write(exception.Message);
                text = " ";
            }
            try
            {
                str2 = item2.SubItems[this.ColumnToSort].Text;
            }
            catch (Exception exception2)
            {
                Console.Write(exception2.Message);
                str2 = " ";
            }
            int num = this.ObjectCompare.Compare(text, str2);
            if (this.OrderOfSort == SortOrder.Ascending)
            {
                return num;
            }
            if (this.OrderOfSort == SortOrder.Descending)
            {
                return -num;
            }
            return 0;
        }

        public SortOrder Order
        {
            get
            {
                return this.OrderOfSort;
            }
            set
            {
                this.OrderOfSort = value;
            }
        }

        public int SortColumn
        {
            get
            {
                return this.ColumnToSort;
            }
            set
            {
                this.ColumnToSort = value;
            }
        }
    }
}

